﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jayden.Dll.ECMAScript
{
	public class If : StatementList
	{
		private Expression m_IfExpression;
		private Statement m_ElseStatement;

		public If(Expression ifExpression, Statement elseStatement)
		{
			m_IfExpression = ifExpression;
			m_ElseStatement = elseStatement;
		}

		public override void RenderStatement(int level, StringBuilder builder)
		{
			StatementList.AppendSpacing(level, "\t", builder);
			RenderStatementWithoutFirstSpacing(level, builder);
		}

		public void RenderStatementWithoutFirstSpacing(int level, StringBuilder builder)
		{
			builder.Append("if (");
			m_IfExpression.RenderExpression(builder);
			builder.AppendLine(") {");
			base.RenderStatement(level + 1, builder);
			if (m_ElseStatement is If)
			{
				If elseIf = m_ElseStatement as If;
				StatementList.AppendSpacing(level, "\t", builder);
				builder.AppendLine("} else ");
				elseIf.RenderStatementWithoutFirstSpacing(level, builder);
			}
			else if (m_ElseStatement != null)
			{
				StatementList.AppendSpacing(level, "\t", builder);
				builder.AppendLine("} else {");
				m_ElseStatement.RenderStatement(level + 1, builder);
			}
			StatementList.AppendSpacing(level, "\t", builder);
			builder.AppendLine("}");
		}
	}
}
